﻿using CubeCube.Object;
using CubeCube.Utils;

namespace CubeCube.Tile
{
    public class DynamicTile : CCObject
    {

        public TileDefinition tileDefinition;
        public IndexPosition tilePosition;

        private TileManager tileManager;

        /*
         * 动态tile用于计时使用,不要修改,只能被TileManager.SetTileDynamicTimeout()使用
         */
        public int timeout;

        private bool invalidated;  //是否无效

        private bool proxy;        //是否为一个代理
        private Tile tile;

        public bool IsProxy
        {
            get { return proxy; }
        }

        public bool OnFire
        {
            get
            {
                if (proxy)
                    return tileManager.GetTile(tilePosition).OnFire;
                else
                    return tile.OnFire;
            }
        }

        public bool CastShadow
        {
            get
            {
                if (proxy)
                    return tileManager.GetTile(tilePosition).CastShadow;
                else
                    return tile.CastShadow;
            }
        }

        public bool LightSource
        {
            get
            {
                if (proxy)
                    return tileManager.GetTile(tilePosition).LightSource;
                else
                    return tile.LightSource;
            }
        }

        public bool Enqueued
        {
            get
            {
                if (proxy)
                    return tileManager.GetTile(tilePosition).Enqueued;
                else
                    return tile.Enqueued;
            }
        }

        public byte ExtraData
        {
            get
            {
                if (proxy)
                    return tileManager.GetTile(tilePosition).ExtraData;
                else
                    return tile.ExtraData;
            }
        }

        public byte AmbientLuminance
        {
            get
            {
                if (proxy)
                    return tileManager.GetTile(tilePosition).AmbientLuminance;
                else
                    return tile.AmbientLuminance;
            }
        }

        public byte LightSourceLuminance
        {
            get
            {
                if (proxy)
                    return tileManager.GetTile(tilePosition).LightSourceLuminance;
                else
                    return tile.LightSourceLuminance;
            }
        }

        public byte Energy
        {
            get
            {
                if (proxy)
                    return tileManager.GetTile(tilePosition).Energy;
                else
                    return tile.Energy;
            }
        }

        public bool Destroyed
        {
            get
            {
                if (proxy)
                    return tileManager.GetTile(tilePosition).Destroyed;
                else
                    return tile.Destroyed;
            }
        }

        public bool Dynamic
        {
            get
            {
                if (proxy)
                    return tileManager.GetTile(tilePosition).Dynamic;
                else
                    return tile.Dynamic;
            }
        }

        //无效
        public bool Invalidated
        {
            get { return this.invalidated; }
            set { this.invalidated = value; }
        }

        public DynamicTile(CubeCube.World.CubeWorld world, TileDefinition tileDefinition, int objectId)
            : base(objectId)
        {
            this.world = world;
            this.definition = tileDefinition;
            this.tileDefinition = tileDefinition;

            this.tile = new Tile(tileDefinition);
            this.tile.Dynamic = true;


        }

        public DynamicTile(CubeCube.World.CubeWorld world, IndexPosition tilePosition, bool proxy, int objectId)
            : base(objectId)
        {
            this.world = world;
            this.tileManager = world.chunkManager.GetTileManager(tilePosition);
            this.tilePosition = tilePosition;
            this.postion = Utils.Graphics.TilePositionToVector3(tilePosition);
            this.proxy = proxy;

            Tile tile = tileManager.GetTile(tilePosition);

            this.tileDefinition = tileManager.GetTileDefinition(tile.tileType);
            this.definition = this.tileDefinition;

            if (proxy == false)
            {
                this.tile = tile;
            }
        }

        public Tile ToTile()
        {
            Tile totile = this.tile;
            totile.tileType = this.tileDefinition.tileType;
            return totile;
        }

        private bool insideUpdate; //是否处于update中
        private bool makeStatic = false; //当处于Update的时候不能立即makeStatic，必须添加标记

        public void MakeStatic()
        {
            if (insideUpdate)
                makeStatic = true;
            else
                tileManager.SetTileDynamic(tilePosition, false, false, 0);
        }

        public override void Update(float deltaTime)
        {
            insideUpdate = true;

            base.Update(deltaTime);

            insideUpdate = false;

            if (makeStatic)
                MakeStatic();
        }

        public void Invalidate()
        {
            invalidated = true;
        }
    }
}
